<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;

use think\Db;
class AdminMsgController extends AdminBaseController
{
      public function index(){
          $msg = Db::name('info')->paginate(2);
          //var_dump($articles->toArray());die;
          // 获取分页显示
          $page = $msg->render();

          $this->assign("page", $page);
          $this->assign('msg',$msg);
          return $this->fetch();
      }

    public function delete()
    {
        $data = $this->request->param();
        $result =$this->adminDeleteMsg($data);

        if ($result) {
            $this->success(lang('DELETE_SUCCESS'));
        } else {
            $this->error(lang('DELETE_FAILED'));
        }

    }

    public function adminDeleteMsg($data)
    {

        if (isset($data['id'])) {
            $id = $data['id']; //获取删除id
            if ($id) {
                Db::startTrans(); //开启事务
                $transStatus = false;
                try {
                    Db::name('info')->where('id',$id)->delete();

                    $transStatus = true;
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {

                    // 回滚事务
                    Db::rollback();
                }
                return $transStatus;

            } else {
                return false;
            }
        } elseif (isset($data['ids'])) {
            $ids = $data['ids'];
            if ($ids) {
                Db::startTrans(); //开启事务
                $transStatus = false;
                try {
                    Db::name('info')->where(['id' => ['in', $ids]])->delete();

                    $transStatus = true;
                    // 提交事务
                    Db::commit();

                } catch (\Exception $e) {

                    // 回滚事务
                    Db::rollback();


                }
                return $transStatus;

            } else {
                return false;
            }

        } else {
            return false;
        }
    }



}